<?php
if (!defined('MINI_RUN')) exit();
/**
 * 连接MySQL数据库的PDO抽象对象类
 * @author Ready
 * @since 2011-8-26
 */
class MysqlPDO
{	
	private static $dbtype = 'mysql';
	private static $dbHost = 'localhost';
	private static $dbUser = 'mini2011';
	private static $dbPass = 'accp';
	private static $dbName = 'CodePlayer';
	private static $dbPort = '3306';
	private static $charset = 'utf8';
	
	private $connection;
	
	/**
	 * 获取MySQL的PDO连接对象
	 */
	public function getConnection(){
		if ($this->connection instanceof PDO){
			return $this->connection;
		}
		$dsn = self::$dbtype.':host='.self::$dbHost.';port='.self::$dbPort.';dbname='.self::$dbName.';charset='.self::$charset;
		try {
			$this->connection = new PDO($dsn, self::$dbUser, self::$dbPass);						
			$this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
			$this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING);
			$this->connection->query('set names '.self::$charset);			
		}catch (PDOException $e){
			exit('连接数据库失败！Error in get Connection,信息：'.$e->getMessage());			
		}
		return $this->connection;
	}
	
	/**
	 * 关闭MySQL数据库连接
	 */
	public function close(){
		try {
			if ($this->connection){
				$this->connection = null;
			}
		}catch (Exception $e){
			exit('关闭数据库连接失败！Error in close Connection,信息：'.$e->getMessage());
		}
	}
	/**
	 * 执行插入的SQL命令并返回自增长ID
	 * @param string $sql
	 * @param array $parameter
	 */
	public function insertSQL($sql, $parameter = null){		
		try {
			$conn = $this->getConnection();
			$stmt = $conn->prepare($sql);
			if (is_array($parameter))
				$stmt->execute($parameter);
			else
				$stmt->execute();
			return $this->connection->lastInsertId();			
		}catch (Exception $e){
			exit('Error in update SQL :'.$e->getMessage());
		}
	}
	
	/**
	 * 执行插入、更新或者删除数据的SQL命令， 返回受影响的行数
	 * @param string $sql
	 * @param array $parameter
	 */
	public function changeSQL($sql, $parameter = null){		
		try {
			$stmt = $this->getConnection()->prepare($sql);
			if (is_array($parameter))
				$stmt->execute($parameter);
			else
				$stmt->execute();
			return $stmt->rowCount();						
		}catch (Exception $e){
			exit('Error in update SQL :'.$e->getMessage());
		}		
	}
	
	/**
	 * 执行查询的SQL命令，并返回关联数组形式的结果数组
	 * @param string $sql
	 * @param array $parameter
	 */
	public function selectSQL($sql, $parameter = null){
		try {
			$stmt = $this->getConnection()->prepare($sql);
			if (is_array($parameter))
				$stmt->execute($parameter);
			else
				$stmt->execute();
			$stmt->setFetchMode(PDO::FETCH_ASSOC);			
			return $stmt->fetchAll();
		} catch (Exception $e) {
			exit('Error in select SQL :'.$e->getMessage());
		}
	}
	
	/**
	 * 执行查询的SQL命令并返回对象形式的结果数组
	 * @param string $sql
	 * @param array $parameter
	 * @param string $classname
	 */
	public function getObjectArray($sql, $parameter = null, $className){		
		try {
			$stmt = $this->getConnection()->prepare($sql);
			if (is_array($parameter))
				$stmt->execute($parameter);
			else 
				$stmt->execute();
			return  $stmt->fetchAll(PDO::FETCH_CLASS,$className);			
		} catch (Exception $e) {
			exit('Error in get Object Array :'.$e->getMessage());
		}
	}
}

?>